package com.easy.order.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.easy.bean.Orders;
import com.easy.bean.Users;
import com.easy.common.CommonResult;
import com.easy.config.SYSConfig;
import com.easy.order.service.UserFeignService;
import com.easy.order.service.impl.OrderServiceImpl;

@RestController
@RequestMapping("order")
@CrossOrigin
public class OrderController {
    @Autowired
    UserFeignService userFS;
    @Autowired
    OrderServiceImpl orderS;

    @RequestMapping(method = RequestMethod.POST, path = "/")
    public CommonResult insertOrder(HttpServletRequest request, @RequestBody Orders order) {
        String token = request.getHeader(SYSConfig.TOKEN_NAME);
        Users user = userFS.getUser(token);
        order.setUserid(user.getId());

        int orderid=orderS.insertOrder(order);
        //1.在订单表中插入数据
        //2.插入订单明细
        //3.插入收货地址
        //4.删除购物车中对应的商品数据
        return CommonResult.success(orderid);
    }
    @GetMapping("orderlistofuser")
    public CommonResult getOrderListOfUser(HttpServletRequest request) {
    	String token=request.getHeader(SYSConfig.TOKEN_NAME);
    	Users user=userFS.getUser(token);
    	List orderList=orderS.getOrderListByUser( user);
    	return CommonResult.success(orderList);
    }
    @GetMapping("detail/{id}")
    public CommonResult getOrderDetailByOrderID(@PathVariable int id) {
    	Orders order=orderS.getOrderInfo(id);
    	return CommonResult.success(order);
    }
    
    
}
